<template>
  <h1>员工列表综合练习</h1>

  <el-form label-width="80px">
      <el-form-item label="姓名">
        <el-input placeholder="请输入姓名" v-model="user.name"></el-input>
      </el-form-item>
    <el-form-item label="工资">
      <el-input placeholder="请输入工资" v-model="user.salary"></el-input>
    </el-form-item>
    <el-form-item label="岗位">
      <el-input placeholder="请输入岗位" v-model="user.post"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="add">点我提交</el-button>
    </el-form-item>
  </el-form>

  <el-table :data="arr">
    <el-table-column label="编号" type="index"></el-table-column>
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="salary" label="工资"></el-table-column>
    <el-table-column prop="post" label="岗位"></el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="danger" @click="del(scope.row,scope.$index)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

</template>


<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";

const user = ref({
  name: '',
  salary: '',
  post: ''
});

const arr=ref([
    {name: '张三', salary: 1000, post: '前端工程师'},
    {name: '李四', salary: 2000, post: '后端工程师'},
    {name: '王五', salary: 3000, post: '产品经理'},
]);

const add = () => {
  if(user.value.name.trim()==''||user.value.salary.trim()==''||user.value.post.trim()==''){
    ElMessage.error('请输入员工信息!');
    return;
  }
  arr.value.push(user.value);
  user.value={
    name: '',
    salary: '',
    post: ''
  }
}

const del = (row,index) => {
  if(confirm('您确认要删除'+row.name+'吗？')){
    arr.value.splice(index,1);
    ElMessage.success('删除成功！');
  }
}
</script>


<style scoped>

</style>